package com.zonda.sampleretrofit2.view;

import android.support.v4.util.ArrayMap;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

public class ViewRecycler<V extends View> implements IBaseReleaser {

  private static final String LOG_TAG = "ViewRecycler";

  private ArrayMap<String, ArrayList<V>> mScrapCollections;

  public final void addScrapView(V scrapView) {

    if (mScrapCollections == null) {
      mScrapCollections = new ArrayMap<>(3);
    }
    final String keyName = scrapView.getClass().getName();
    Log.i(LOG_TAG, "addScrapView keyName: " + keyName);
    ArrayList<V> specifiedScrapViews = mScrapCollections.get(keyName);
    if (specifiedScrapViews == null) {
      specifiedScrapViews = new ArrayList<>();
      mScrapCollections.put(keyName, specifiedScrapViews);
    }
    specifiedScrapViews.add(scrapView);
  }

  public final V getScrapView(Class<V> scrapName) {

    if (mScrapCollections == null) {
      return null;
    }
    final String keyName = scrapName.getName();
    final ArrayList<V> specifiedScrapViews = mScrapCollections.get(keyName);
    Log.i(LOG_TAG, "getScrapView keyName: " + keyName);
    if (specifiedScrapViews == null || specifiedScrapViews.isEmpty()) {
      return null;
    }
    return specifiedScrapViews.remove(0);
  }

  @Override
  public void freeMemory() {

  }

  @Override
  public void destroy() {

  }
}
